package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @LogAnno
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        courseService.deleteById(id);
        return Result.success();
    }
    @LogAnno
    @PutMapping
    public Result update(@RequestBody Course course){
        courseService.update(course);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        Course course =courseService.getById(id);
        return Result.success(course);
    }
    @GetMapping
    public Result Page(@RequestParam(name = "name",required = false)String name,
                       @RequestParam(name = "subject",required = false)Integer sucject,
                       @RequestParam(name = "target",required = false)Integer target,
                       @RequestParam(name = "page",defaultValue = "1") Integer page,
                       @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize){
        PageResult<Course> pageResult =courseService.page(name,sucject,target,page,pageSize);
        return Result.success(pageResult);
    }
    @LogAnno
    @PostMapping
    public Result add(@RequestBody Course course){
        courseService.add(course);
        return Result.success();
    }
    @GetMapping("/subject/{subject}")
    public Result listBySubject(@PathVariable Integer subject){
        return Result.success(courseService.listBySubject(subject));
    }
    @GetMapping("/list")
    public Result getAllCourses(){
        List<Course> courses =courseService.getAllCourses();
        return Result.success(courses);
    }
}
